Conditions | 1 |
Paths | 1 |
Total Lines | 99 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | 'use strict'; |
||
13 | var ioEvents = function(io) { |
||
14 | |||
15 | // Rooms namespace |
||
16 | io.of('/rooms').on('connection', function(socket) { |
||
17 | |||
18 | // Create a new room |
||
19 | socket.on('createRoom', function(title) { |
||
20 | Room.findOne({'title': new RegExp('^' + title + '$', 'i')}, function(err, room){ |
||
21 | if(err) throw err; |
||
|
|||
22 | if(room){ |
||
23 | socket.emit('updateRoomsList', { error: 'Room title already exists.' }); |
||
24 | } else { |
||
25 | Room.create({ |
||
26 | title: title |
||
27 | }, function(err, newRoom){ |
||
28 | if(err) throw err; |
||
29 | socket.emit('updateRoomsList', newRoom); |
||
30 | socket.broadcast.emit('updateRoomsList', newRoom); |
||
31 | }); |
||
32 | } |
||
33 | }); |
||
34 | }); |
||
35 | }); |
||
36 | |||
37 | // Chatroom namespace |
||
38 | io.of('/chatroom').on('connection', function(socket) { |
||
39 | |||
40 | // Join a chatroom |
||
41 | socket.on('join', function(roomId) { |
||
42 | Room.findById(roomId, function(err, room){ |
||
43 | if(err) throw err; |
||
44 | if(!room){ |
||
45 | // Assuming that you already checked in router that chatroom exists |
||
46 | // Then, if a room doesn't exist here, return an error to inform the client-side. |
||
47 | socket.emit('updateUsersList', { error: 'Room doesnt exist.' }); |
||
48 | } else { |
||
49 | // Check if user exists in the session |
||
50 | if(socket.request.session.passport == null){ |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | Room.addUser(room, socket, function(err, newRoom){ |
||
55 | |||
56 | // Join the room channel |
||
57 | socket.join(newRoom.id); |
||
58 | |||
59 | Room.getUsers(newRoom, socket, function(err, users, cuntUserInRoom){ |
||
60 | if(err) throw err; |
||
61 | |||
62 | // Return list of all user connected to the room to the current user |
||
63 | socket.emit('updateUsersList', users, true); |
||
64 | |||
65 | // Return the current user to other connecting sockets in the room |
||
66 | // ONLY if the user wasn't connected already to the current room |
||
67 | if(cuntUserInRoom === 1){ |
||
68 | socket.broadcast.to(newRoom.id).emit('updateUsersList', users[users.length - 1]); |
||
69 | } |
||
70 | }); |
||
71 | }); |
||
72 | } |
||
73 | }); |
||
74 | }); |
||
75 | |||
76 | // When a socket exits |
||
77 | socket.on('disconnect', function() { |
||
78 | |||
79 | // Check if user exists in the session |
||
80 | if(socket.request.session.passport == null){ |
||
81 | return; |
||
82 | } |
||
83 | |||
84 | // Find the room to which the socket is connected to, |
||
85 | // and remove the current user + socket from this room |
||
86 | Room.removeUser(socket, function(err, room, userId, cuntUserInRoom){ |
||
87 | if(err) throw err; |
||
88 | |||
89 | // Leave the room channel |
||
90 | socket.leave(room.id); |
||
91 | |||
92 | // Return the user id ONLY if the user was connected to the current room using one socket |
||
93 | // The user id will be then used to remove the user from users list on chatroom page |
||
94 | if(cuntUserInRoom === 1){ |
||
95 | socket.broadcast.to(room.id).emit('removeUser', userId); |
||
96 | } |
||
97 | }); |
||
98 | }); |
||
99 | |||
100 | // When a new message arrives |
||
101 | socket.on('newMessage', function(roomId, message) { |
||
102 | |||
103 | // No need to emit 'addMessage' to the current socket |
||
104 | // As the new message will be added manually in 'main.js' file |
||
105 | // socket.emit('addMessage', message); |
||
106 | |||
107 | socket.broadcast.to(roomId).emit('addMessage', message); |
||
108 | }); |
||
109 | |||
110 | }); |
||
111 | } |
||
112 | |||
146 | module.exports = init; |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.